package com.lab.widget.pager;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.business.user.proxy.UserInfoProxy;
import com.cuitrip.service.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 自定义tab布局
 * Created by ak
 */
public class LogoTabView extends LinearLayout implements TabPageIndicator.ITabView {

    @Bind(R.id.tab_logo)
    IconTextView mTabLogo;
    @Bind(R.id.tab_text)
    TextView mTabText;
    @Bind(R.id.tab_pointer)
    ImageView tabPointer;
    private Context mContext;
    private int mIndex;
    private int mMaxTabWidth;

    public LogoTabView(Context context) {
        this(context, null);
    }

    public LogoTabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LogoTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initInternal(attrs, context, defStyleAttr);
    }

    private void initInternal(AttributeSet attrs, Context context, int defStyle) {
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.tab_view, this);
        ButterKnife.bind(this);
        if (UserInfoProxy.getInstance().isTraveller()){
            mTabLogo.setTextColor(getResources().getColorStateList(R.color.tab_orange_indicator_text));
            mTabText.setTextColor(getResources().getColorStateList(R.color.tab_orange_indicator_text));
        }else{
            mTabLogo.setTextColor(getResources().getColorStateList(R.color.tab_blue_indicator_text));
            mTabText.setTextColor(getResources().getColorStateList(R.color.tab_blue_indicator_text));
        }
    }

    public void setTabLogo(CharSequence text){
        if (TextUtils.isEmpty(text)) {
            mTabLogo.setVisibility(GONE);
        } else {
            mTabLogo.setVisibility(VISIBLE);
            mTabLogo.setText(text);
        }
    }

    public void setTabText(CharSequence text){
        mTabText.setText(text);
    }

    public int getIndex() {
        return mIndex;
    }

    public void setIndex(int index) {
        this.mIndex = index;
    }

    public void setmMaxTabWidth(int maxTabWidth){
        mMaxTabWidth = maxTabWidth;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Re-measure if we went beyond our maximum size.
        if (mMaxTabWidth > 0 && getMeasuredWidth() > mMaxTabWidth) {
            super.onMeasure(MeasureSpec.makeMeasureSpec(mMaxTabWidth, MeasureSpec.EXACTLY),
                    heightMeasureSpec);
        }
    }

}
